/* 
Set directory 
*/

    global root 	ENTER DIRECTORY
                                
	global crops	$root\Analysis\Dta\ICRISAT
	
	global output 	ENTER DIRECTORY
			   
use "$crops\icrisat_crops_analysis.dta", clear

tab krain, gen(krain)
tab nkrain, gen(nkrain)

label var lyield "Log Yield: Top 6 Crops"
label var lyield_m "Log Yield: Top 5 Monsoon Crops"

label var klbin1 "GS Days $<$13C"
label var klbin2 "GS Days 13-15C"
label var klbin3 "GS Days 15-17C"
label var klbin4 "GS Days 17-19C"
label var klbin5 "GS Days 19-21C"
label var klbin6 "GS Days 21-23C"
label var klbin7 "GS Days 23-25C"
label var klbin8 "GS Days 25-27C"
label var klbin9 "GS Days 27-29C"
label var klbin10 "GS Days $>$29C"

label var nklbin1 "NGS Days $<$13C"
label var nklbin2 "NGS Days 13-15C"
label var nklbin3 "NGS Days 15-17C"
label var nklbin4 "NGS Days 17-19C"
label var nklbin5 "NGS Days 19-21C"
label var nklbin6 "NGS Days 21-23C"
label var nklbin7 "NGS Days 23-25C"
label var nklbin8 "NGS Days 25-27C"
label var nklbin9 "NGS Days 27-29C"
label var nklbin10 "NGS Days $>$29C"

*Figure 4 (Panel C and Panel D)
reg lyield klbin1 klbin2 klbin4 klbin5 klbin6 klbin7 klbin8 klbin9 klbin10 nklbin1 nklbin2 nklbin4 nklbin5 nklbin6 nklbin7 nklbin8 nklbin9 nklbin10 krain1 krain3 nkrain1 nkrain3 i.district i.year, cluster(district)
estimates store cT`table'C`col++'

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binkcoef`num' = _b[klbin`num']
gen binkcoefcil`num' = _b[klbin`num'] - 1.96*_se[klbin`num']
gen binkcoefciu`num' = _b[klbin`num'] + 1.96*_se[klbin`num']
}

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binnkcoef`num' = _b[nklbin`num']
gen binnkcoefcil`num' = _b[nklbin`num'] - 1.96*_se[nklbin`num']
gen binnkcoefciu`num' = _b[nklbin`num'] + 1.96*_se[nklbin`num']
}

gen binkcoef3 = 0
gen binkcoefcil3 = 0
gen binkcoefciu3 = 0

gen binnkcoef3 = 0
gen binnkcoefcil3 = 0
gen binnkcoefciu3 = 0

preserve

keep binnkcoef* binnkcoefcil* binnkcoefciu* binkcoef* binkcoefcil* binkcoefciu*
gen id = _n

keep if id == 1

reshape long binnkcoef binnkcoefcil binnkcoefciu binkcoef binkcoefcil binkcoefciu, i(id) j(ll1lbin)

label var binnkcoefcil "95% CI"
label var binnkcoefciu "95% CI"
label var binkcoefcil "95% CI"
label var binkcoefciu "95% CI"

label var ll1lbin "Daily Average Temperature (C)"

twoway (connected binkcoefcil binkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black))  mcolor(red red) lpattern(longdash longdash) lcolor(red red) sort) (connected binkcoef ll1lbin, sort msymbol(D) lcolor(red) mcolor(red) ) ///
(connected binnkcoefcil binnkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black))  mcolor(blue blue) lpattern(shortdash shortdash) lcolor(blue blue) sort) (connected binnkcoef ll1lbin, sort msymbol(O) lcolor(blue) mcolor(blue)) ///
, graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (6 5 3 2)  label(6 "NGS Estimate") label(5 "NGS 95% CI") label(3 "GS Estimate") label(2 "GS 95% CI")) ytitle("Log Yields: Top 6 Crops") ///
xlabel(1 "<13C" 2 "13-15C" 3 "15-17C" 4 "17-19C" 5 "19-21C" 6 "21-23C" 7 "23-25C" 8 "25-27C" 9 "27-29C" 10 ">29C") ylabel(-0.015(0.005)0.005,angle (30))   
graph export "$output/F4c.png", replace width(800) height(600)	

restore

drop binnkcoef* binkcoef*

reg lyield_m klbin1 klbin2 klbin4 klbin5 klbin6 klbin7 klbin8 klbin9 klbin10 nklbin1 nklbin2 nklbin4 nklbin5 nklbin6 nklbin7 nklbin8 nklbin9 nklbin10 krain1 krain3 nkrain1 nkrain3 i.district i.year, cluster(district)
estimates store cT`table'C`col++'


foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binkcoef`num' = _b[klbin`num']
gen binkcoefcil`num' = _b[klbin`num'] - 1.96*_se[klbin`num']
gen binkcoefciu`num' = _b[klbin`num'] + 1.96*_se[klbin`num']
}

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binnkcoef`num' = _b[nklbin`num']
gen binnkcoefcil`num' = _b[nklbin`num'] - 1.96*_se[nklbin`num']
gen binnkcoefciu`num' = _b[nklbin`num'] + 1.96*_se[nklbin`num']
}

gen binkcoef3 = 0
gen binkcoefcil3 = 0
gen binkcoefciu3 = 0

gen binnkcoef3 = 0
gen binnkcoefcil3 = 0
gen binnkcoefciu3 = 0

preserve

keep binnkcoef* binnkcoefcil* binnkcoefciu* binkcoef* binkcoefcil* binkcoefciu*
gen id = _n

keep if id == 1

reshape long binnkcoef binnkcoefcil binnkcoefciu binkcoef binkcoefcil binkcoefciu, i(id) j(ll1lbin)

label var binnkcoefcil "95% CI"
label var binnkcoefciu "95% CI"
label var binkcoefcil "95% CI"
label var binkcoefciu "95% CI"

label var ll1lbin "Daily Average Temperature (C)"

twoway (connected binkcoefcil binkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black))  mcolor(red red) lpattern(longdash longdash) lcolor(red red) sort) (connected binkcoef ll1lbin, sort msymbol(D) lcolor(red) mcolor(red) ) ///
(connected binnkcoefcil binnkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black))  mcolor(blue blue) lpattern(shortdash shortdash) lcolor(blue blue) sort) (connected binnkcoef ll1lbin, sort msymbol(O) lcolor(blue) mcolor(blue)) ///
, graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (6 5 3 2)  label(6 "NGS Estimate") label(5 "NGS 95% CI") label(3 "GS Estimate") label(2 "GS 95% CI")) ytitle("Log Yields: Top 5 Monsoon Crops") ///
xlabel(1 "<13C" 2 "13-15C" 3 "15-17C" 4 "17-19C" 5 "19-21C" 6 "21-23C" 7 "23-25C" 8 "25-27C" 9 "27-29C" 10 ">29C") ylabel(-0.015(0.005)0.005,angle (30)) 
graph export "$output/F4d.png", replace width(800) height(600)	

restore

drop binnkcoef* binkcoef*
